package datastructure.string;

/**
 *
 * @author Lei
 */
public class RemoveDuplicateCharacter {

    // skip the duplicate
    // tailIndex is the final result length
    public static String removeDuplicateCharacter(String inputString) {
        if (inputString == null || inputString.trim().isEmpty()) {
            return null;
        }
        char[] chars = inputString.toCharArray();
        int tailIndex = 1;
        for (int i = 1; i < chars.length; i++) {
            boolean bFoundDuplicate = false;
            for (int j = 0; j < tailIndex; j++) {
                if (chars[j] == chars[i]) {
                    bFoundDuplicate = true;
                    break;
                }
            }

            if (!bFoundDuplicate) {
                chars[tailIndex] = chars[i];
                tailIndex++;
            }
        }

        // return string
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < tailIndex; i++) {
            sb.append(chars[i]);
        }

        return sb.toString();
    }

    public static void main(String[] args) {

      //  String str = "avaaccd";
        String str = "aaa";
        System.out.println(RemoveDuplicateCharacter.removeDuplicateCharacter(str));


    }
}
